﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb.Web.UI;
using System.Collections.Generic;
using Inventory.BusinessEntities;
using Locator.Properties;
using System.Web.Script.Serialization;
using DataAccess.BusinessEntities;

namespace Locator.Inventory.Views
{
    public partial class Inventory : UserControl, IInventoryView
    {
        /// <summary>
        /// 
        /// </summary>
        private InventoryPresenter presenter;

        /// <summary>
        /// 
        /// </summary>
        private List<StoreWithInventory> inventory;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.presenter.OnViewInitialized();
            }

            this.presenter.OnViewLoaded();
        }

        /// <summary>
        /// Gets or sets the presenter.
        /// </summary>
        /// <value>
        /// The presenter.
        /// </value>
        [CreateNew]
        public InventoryPresenter Presenter
        {
            get { return this.presenter; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this.presenter = value;
                this.presenter.View = this;
            }
        }

        /// <summary>
        /// Sets the search results.
        /// </summary>
        /// <param name="inventory">The inventory.</param>
        public void SetSearchResults(List<StoreWithInventory> inventory)
        {
            this.inventory = inventory;
            this.InventoryDataSource.DataSource = this.inventory;
        }

        /// <summary>
        /// Gets the map key.
        /// </summary>
        protected string MapKey
        {
            get { return Resources.MapKey; }
        }

        /// <summary>
        /// Gets the results as json.
        /// </summary>
        public string ResultsAsJson
        {
            get
            {
                string result = new JavaScriptSerializer().Serialize(this.inventory);
                return result;
            }
        }

        /// <summary>
        /// Sets the stores.
        /// </summary>
        /// <param name="stores">The stores.</param>
        public void SetStores(List<Store> stores)
        {
            this.StoresDataSource.DataSource = stores;
        }

        public void SetProducts(List<ProductListItem> products)
        {
            this.ProductsDataSource.DataSource = products;
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            this.presenter.OnSearchClicked();
        }

        public int StoreNumber
        {
            get { return Convert.ToInt32(this.StoreDropDownList.SelectedValue); }
        }

        public int ProductNumber
        {
            get { return Convert.ToInt32(this.ProductDropDownList.SelectedValue); }
        }
    }
}

